package middle;

public class Solution_64 {

    public int minPathSum(int[][] grid) {
        int r = grid.length, c = grid[0].length;
        int[][] arrays = new int[r][c];
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (i == 0 && j == 0) {
                    arrays[i][j] = grid[i][j];
                } else {
                    if (i == 0) {
                        arrays[i][j] = arrays[i][j - 1] + grid[i][j];
                    } else if (j == 0) {
                        arrays[i][j] = arrays[i - 1][j] + grid[i][j];
                    } else {
                        arrays[i][j] = Math.min(arrays[i - 1][j], arrays[i][j - 1]) + grid[i][j];
                    }
                }
            }
        }
        return arrays[r - 1][c - 1];
    }
}
